﻿using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Text;

namespace ReaderExtractTool.Service
{
    public class BeqegeService : BaseService
    {
        public override string GetChapterContent(Chapter chapter, string unnecessaryContentStart)
        {
            throw new NotImplementedException();
        }

        public override List<Chapter> GetChapters(string url, ref string novelTitle)
        {
            List<Chapter> chapterList = new List<Chapter>();

            //var doc = new HtmlWeb().Load("https://www.beqege.com/61467/819561.html");
            //var node = doc.DocumentNode;
            //var result = node.SelectNodes(".//div[@id='list']/dl");
            //if (result == null || result.Count == 0) throw new Exception("未检测到章节目录");

            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("https://www.beqege.com/61467/819561.html");
            myReq.Method = "GET";
            myReq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            myReq.Headers.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
            myReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; QQWubi 133; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; CIBA; InfoPath.2)";

            HttpWebResponse result = (HttpWebResponse)myReq.GetResponse();
            Stream resultStream = result.GetResponseStream();
            StreamReader sr = new StreamReader(new GZipStream(resultStream, CompressionMode.Decompress), System.Text.Encoding.UTF8);
            string html = sr.ReadToEnd();
            sr.Close();
            resultStream.Close();


            return chapterList;
        }
    }
}
